﻿using System;
using System.Collections.Generic;

using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace SceneEngine
{
    public class LineRenderer
    {
        private int vertexCount;
        private int capacity;

        private VertexPositionColor[] vertices;

        private BasicEffect effect;

        public BasicEffect Effect
        {
            get { return effect; }
        }

        public int PrimitiveCount
        {
            get { return (vertexCount / 2); }
        }

        public LineRenderer(int capacity)
        {
            this.capacity = capacity;

            vertices = new VertexPositionColor[capacity];

            effect = new BasicEffect(EngineGlobals.GraphicsDevice);

            effect.LightingEnabled = false;
            effect.VertexColorEnabled = true;
        }

        public void Clear()
        {
            vertexCount = 0;
        }

        protected void SubmitVertex(Vector3 position, Color color)
        {
            vertices[vertexCount].Position = position;
            vertices[vertexCount++].Color = color;
        }

        public void Submit(Vector3 start, Vector3 end, Color color)
        {
            SubmitVertex(start, color);
            SubmitVertex(end, color);
        }

        public void Render(Matrix view, Matrix projection)
        {
            if (vertexCount >= 2)
            {
                effect.View = view;
                effect.Projection = projection;

                effect.CurrentTechnique.Passes[0].Apply();

                EngineGlobals.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                    PrimitiveType.LineList, vertices, 0, vertexCount / 2);
            }
        }
    }
}
